package com.eqdd.yiqidian.ui.tongxunlu.stafftree

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.eqdd.floatingmenu.MenuItem
import com.eqdd.floatingmenu.TopRightMenu
import com.eqdd.library.percentUtil.WindowUtil
import com.eqdd.yiqidian.R
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.DepartmentStaffTreeBean
import com.eqdd.yiqidian.ui.discover.workdiary.DepartmentWorkDiaryActivity
import com.eqdd.yiqidian.ui.mine.UserCardActivity
import com.eqdd.yiqidian.utils.IMUtil
import com.eqdd.yiqidian.utils.ImageUtil
import com.jhj.datalibrary.tree.single.SimpleSingleTreeAdapter
import kotlinx.android.synthetic.main.layout_single_tree_node.view.*
import org.jetbrains.anko.sdk25.coroutines.onLongClick
import org.jetbrains.anko.startActivity
import java.util.*

/**
 *
 * Created by jhj on 18-5-9.
 */
class StaffTreeSingleAdapter(val mContext: Context) : SimpleSingleTreeAdapter<DepartmentStaffTreeBean, StaffTreeSingleAdapter.ItemViewHolder>() {


    var type = ""

    override val context: Context
        get() = mContext
    override val reminder: String
        get() = "没有员工"


    override fun onBindItemViewHolder(holder: ItemViewHolder, data: DepartmentStaffTreeBean, position: Int) {
        holder.itemView.let {
            it.tv_name.text = data.name
            it.tv_id.text = data.department
            if (data.isRoot) {
                it.checkbox.visibility = android.view.View.GONE
            } else {
                it.checkbox.visibility = android.view.View.VISIBLE
                ImageUtil.setCircleImage(data.icon, it.iv_tree_mark)
            }
            if (selectedItem?.name == data.name && selectedItem?.id == data.id) {
                data.isChecked = true
            }
            it.checkbox.visibility = View.GONE
            if (data.isRoot) {
                if (data.isShowChildren) {
                    it.iv_tree_mark.setImageResource(R.drawable.tree_on)
                } else {
                    it.iv_tree_mark.setImageResource(R.drawable.tree_off)
                }
            }
        }
    }

    override fun onCreateItemView(parent: ViewGroup?, viewType: Int): View {
        val inflater = LayoutInflater.from(parent?.context)
        return if (viewType == 1) {
            inflater.inflate(R.layout.layout_single_tree_root, parent, false)
        } else {
            inflater.inflate(R.layout.layout_single_tree_node, parent, false)
        }
    }

    override fun onCreateItemViewHolder(view: View): ItemViewHolder {
        return ItemViewHolder(view)
    }


    inner class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        init {
            when (type) {
            //通讯录中同事列表
                StaffTreeSingleActivity.TYPE_TONGXUNLU -> {
                    itemView.setOnClickListener {
                        val data = itemView.tag as DepartmentStaffTreeBean
                        itemViewOnClick(data)
                        if (!data.isRoot) {
                            (mContext as Activity).startActivity<UserCardActivity>(Config.GUID to data.id)
                        }
                    }
                    itemView.onLongClick {
                        val data = itemView.tag as DepartmentStaffTreeBean
                        if (!data.isRoot) {
                            val mTopRightMenu = TopRightMenu(mContext as Activity)
                            val menuItems = ArrayList<MenuItem>()
                            menuItems.add(MenuItem("发送消息"))
                            menuItems.add(MenuItem("拨打电话"))
                            mTopRightMenu
                                    .setHeight(WindowUtil.csw * 110 / WindowUtil.width)     //默认高度480
                                    .setWidth(WindowUtil.csw * 150 / WindowUtil.width)      //默认宽度wrap_content
                                    .showIcon(false)     //显示菜单图标，默认为true
                                    .dimBackground(true)        //背景变暗，默认为true
                                    .needAnimationStyle(true)   //显示动画，默认为true
                                    .setAnimationStyle(R.style.TRM_ANIM_STYLE)
                                    .addMenuList(menuItems)
                                    .setOnMenuItemClickListener { position ->
                                        println()
                                        when (position) {
                                            0 -> IMUtil.startPrivate(mContext, data.id, data.name, data.icon)
                                            1 -> {
                                                val intent = Intent(Intent.ACTION_DIAL)
                                                val url = Uri.parse("tel:" + data.phone)
                                                intent.data = url
                                                mContext.startActivity(intent)
                                            }
                                        }
                                    }
                            mTopRightMenu.showAsDropDown(itemView, 200, -20)    //带偏移量
                        }
                    }
                }
            //企业动态中部门日志中的部门下员工
                StaffTreeSingleActivity.TYPE_DEPARTMENT_DIARY -> {
                    itemView.setOnClickListener {
                        val data = itemView.tag as DepartmentStaffTreeBean
                        itemViewOnClick(data)
                        if (!data.isRoot)
                            mContext.startActivity<DepartmentWorkDiaryActivity>(Config.GUID to data.id)
                    }

                }
            StaffTreeSingleActivity.TYPE_SINGLE_SELECT -> {
                    itemView.setOnClickListener {
                        val data = itemView.tag as DepartmentStaffTreeBean
                        itemViewOnClick(data)
                        if (!data.isRoot) {
                            val intent = Intent()
                            intent.putExtra("data", data)
                            (mContext as Activity).setResult(Activity.RESULT_OK, intent)
                            mContext.finish()
                        }
                    }
                }

            }

        }
    }
}

